/* 标准api调用函数库,严格与后端接口一致 */
// service backend api

/* eslint-disable camelcase */

import { axiosService } from 'boot/axios'

// 定义 MeetingRoomConfig 类型 (基于文档)
interface MeetingRoomConfig {
  type_50_num?: number;
  type_50_visible?: boolean;
  type_100_num?: number;
  type_100_visible?: boolean;
  type_300_num?: number;
  type_300_visible?: boolean;
  type_500_num?: number;
  type_500_visible?: boolean;
  type_1000_num?: number;
  type_1000_visible?: boolean;
}

// 定义响应体类型 (基于文档)
export interface MeetingRoomConfigResponse {
  room_info: {
    id: string;
    creation_time: string;
    update_time: string;
    type_50_num: number;
    type_50_visible: boolean;
    type_100_num: number;
    type_100_visible: boolean;
    type_300_num: number;
    type_300_visible: boolean;
    type_500_num: number;
    type_500_visible: boolean;
    type_1000_num: number;
    type_1000_visible: boolean;
  };
}

// 定义会议预定时间配置响应体类型
export interface ReserveTimesConfigResponse {
  reserve_room_advance_minutes: number;
  meeting_after_reserve_minutes: number;
  default_advance_notice_minutes: number;
}

// 定义更新会议预定时间配置的请求体类型
interface UpdateReserveTimesConfigPayload {
  reserve_room_advance_minutes?: number;
  meeting_after_reserve_minutes?: number;
  default_advance_notice_minutes?: number;
}

// 定义会议室预定区间统计的查询参数类型
interface MeetingRoomIntervalsParams {
  start_time: number;
  end_time: number;
  room_type?: 50 | 100 | 300 | 500 | 1000;
}

// 定义会议室预定区间统计的响应体类型
interface MeetingRoomIntervalsResponse {
  start_time: number;
  end_time: number;
  data: {
    room_type: number;
    intervals: {
      start: number;
      end: number;
      reserved: number;
      total: number;
    }[];
  }[];
}

// 定义查询可用会议室的查询参数类型 (与区间统计相同)
type UsableMeetingRoomsParams = MeetingRoomIntervalsParams;

// 定义查询可用会议室的响应体类型
interface UsableMeetingRoomsResponse {
  start_time: number;
  end_time: number;
  count_start: number;
  count_end: number;
  data: {
    room_type: number;
    reserved: number;
    total: number;
  }[];
}

// 定义分页查询参数类型
export interface PaginationParams {
  page?: number;
  page_size?: number;
}

// 定义用户角色类型 (移到 MeetingUser 之前)
export type UserRole = 'admin' | 'regular';

// 定义用户状态类型 (移到 MeetingUser 之前)
export type UserStatus = 'active' | 'inactive';

// 定义会议预定用户更新请求体类型
export interface MeetingUserUpdate {
  /**
   * 电话
   */
  telephone: string;
  /**
   * 公司、单位
   */
  company: string;
  /**
   * 名
   */
  first_name: string;
  /**
   * 姓
   */
  last_name: string;
}

// 定义会议用户类型
export interface MeetingUser {
  id: string;
  role: UserRole;
  status: UserStatus;
  creation_time: string;
  update_time: string;
  user: {
    id: string;
    username: string;
    fullname: string;
    telephone: string;
    company: string;
  };
}

// 定义会议室类型
export type RoomType = 50 | 100 | 300 | 500 | 1000;

// 定义预定状态类型
export type ReservationStatus = 'reserved' | 'cancelled';

// 定义列举会议预定的查询参数类型
export interface ListReservationsParams extends PaginationParams {
  status?: ReservationStatus;
  room_type?: RoomType;
  meeting_code?: string;
  start_time_start?: number;
  start_time_end?: number;
  'as-admin'?: boolean; // 以管理员身份请求
}

// 定义邮件通知类型
interface EmailNotice {
  id: string;
  subject: string;
  message: string;
  is_html: boolean;
  sender: string;
  receiver: string;
  status: 'wait' | 'success' | 'failed';
  status_desc: string;
  send_time: string;
  success_time: string | null;
}

// 定义会议预定类型
export interface MeetingReservation {
  id: string;
  subject: string;
  start_time: number;
  end_time: number;
  room_reserve_start: number;
  room_reserve_end: number;
  people_num: number;
  room_type: RoomType;
  status: ReservationStatus;
  creation_time: string;
  update_time: string;
  user: {
    id: string;
    username: string;
    full_name: string;
  };
  tx_start_time: number;
  tx_end_time: number;
  tx_meeting_id: string;
  tx_meeting_userid: string;
  tx_user_nick_name: string;
  tx_meeting_code: string;
  tx_join_url: string;
  room_reserve_status: 'wait' | 'unreserved' | 'reserved' | 'unreleased' | 'released';
  room_reserve_desc: string;
  advance_notice_seconds: number;
  email_notice_id: string | null;
  email_notice?: EmailNotice | null; // 在查询详情时可能包含
}

// 定义列举会议预定的响应体类型
interface ListReservationsResponse {
  count: number;
  page_num: number;
  page_size: number;
  results: MeetingReservation[];
}

// 定义创建会议预定的请求体类型
interface CreateReservationPayload {
  subject: string;
  meeting_code: string;
  start_time: number;
  end_time: number;
  people_num: number;
  room_type: RoomType;
}

// 定义预定须知响应/请求体类型
export interface TermsConditionsPayload {
  content: string;
}

// 定义腾讯会议状态类型
type TencentMeetingState = 1 | 2 | 3;

// 定义查询腾讯会议信息的查询参数类型
interface GetTencentMeetingInfoParams {
  meeting_code: string;
  meeting_state?: TencentMeetingState;
}

// 定义腾讯会议信息类型
interface TencentMeetingInfo {
  meeting_id: string;
  meeting_code: string;
  subject: string;
  userid: string;
  userid_type: number;
  nick_name: string;
  start_time: number;
  end_time: number;
  online_member_num: number;
  meeting_state: TencentMeetingState;
  meeting_type: number;
}

// 定义查询腾讯会议信息的响应体类型
interface GetTencentMeetingInfoResponse {
  current_page: number;
  current_size: number;
  total_count: number;
  total_page: number;
  meeting_info_list: TencentMeetingInfo[];
}

// 定义通过会议号查询腾讯会议信息的响应类型
interface GetMeetingByCodeResponse {
  meeting_number: number;
  meeting_info_list: {
    meeting_id: string;
    meeting_code: string;
    subject: string;
    start_time: number;
    end_time: number;
    meeting_type: number;
  }[];
}

// 定义会议室可预定区间统计的响应体类型
interface MeetingRoomUsableIntervalsResponse {
  start_time: number;
  end_time: number;
  data: {
    room_type: number;
    intervals: {
      start: number;
      end: number;
      reservable: number;
      total: number;
    }[];
  }[];
}

// 定义列举会议用户的响应体类型
interface ListMeetingUsersResponse {
  count: number;
  page_num: number;
  page_size: number;
  results: MeetingUser[];
}

interface ListMeetingUsersParams extends PaginationParams {
  status?: 'active' | 'inactive';
  role?: 'admin' | 'regular';
  search?: string;
}

export default {
  // apis
  txMeeting: {
    /**
     * @desc 查询会议室资源配置
     * @method GET
     * @url /app_tx_meeting/configs/meeting-room
     */
    getMeetingRoomConfig: () => {
      return axiosService.get<MeetingRoomConfigResponse>('/app_tx_meeting/configs/meeting-room')
    },
    /**
     * @desc 修改会议室资源配置
     * @method POST
     * @url /app_tx_meeting/configs/meeting-room/update
     * @param {MeetingRoomConfig} data - 会议室配置数据
     */
    updateMeetingRoomConfig: (data: MeetingRoomConfig) => {
      return axiosService.post<MeetingRoomConfigResponse>('/app_tx_meeting/configs/meeting-room/update', data)
    },
    /**
     * @desc 查询会议预定相关时间的配置
     * @method GET
     * @url /app_tx_meeting/configs/reserve-times
     */
    getReserveTimesConfig: () => {
      return axiosService.get<ReserveTimesConfigResponse>('/app_tx_meeting/configs/reserve-times')
    },
    /**
     * @desc 修改会议预定相关时间的配置
     * @method POST
     * @url /app_tx_meeting/configs/reserve-times/update
     * @param {UpdateReserveTimesConfigPayload} data - 时间配置数据
     */
    updateReserveTimesConfig: (data: UpdateReserveTimesConfigPayload) => {
      return axiosService.post<ReserveTimesConfigResponse>('/app_tx_meeting/configs/reserve-times/update', data)
    },
    /**
     * @desc 会议室资源已预定区间统计
     * @method GET
     * @url /app_tx_meeting/meeting-room/intervals
     * @param {MeetingRoomIntervalsParams} params - 查询参数
     */
    getMeetingRoomIntervals: (params: MeetingRoomIntervalsParams) => {
      return axiosService.get<MeetingRoomIntervalsResponse>('/app_tx_meeting/meeting-room/intervals', { params })
    },
    /**
     * @desc 查询一个时间段内会议室资源可预定数
     * @method GET
     * @url /app_tx_meeting/meeting-room/usable
     * @param {UsableMeetingRoomsParams} params - 查询参数
     */
    getUsableMeetingRooms: (params: UsableMeetingRoomsParams) => {
      return axiosService.get<UsableMeetingRoomsResponse>('/app_tx_meeting/meeting-room/usable', { params })
    },
    /**
     * @desc 会议室资源可预定数区间统计
     * @method GET
     * @url /app_tx_meeting/meeting-room/usable/intervals
     * @param {MeetingRoomIntervalsParams} params - 查询参数
     */
    getUsableMeetingRoomIntervals: (params: MeetingRoomIntervalsParams) => {
      return axiosService.get<MeetingRoomUsableIntervalsResponse>('/app_tx_meeting/meeting-room/usable/intervals', { params })
    },
    /**
     * @desc 列举会议预定的用户 (管理员权限)
     * @method GET
     * @url /app_tx_meeting/meeting-user
     * @param {ListMeetingUsersParams} params - 查询参数，支持分页、状态、角色和用户名检索
     */
    listMeetingUsers: (params?: ListMeetingUsersParams) => {
      return axiosService.get<ListMeetingUsersResponse>('/app_tx_meeting/meeting-user', { params })
    },
    /**
     * @desc 查询用户在会议预定服务中个人的信息和角色
     * @method GET
     * @url /app_tx_meeting/meeting-user/personal
     */
    getPersonalMeetingUserInfo: () => {
      // 响应体是单个 MeetingUser
      return axiosService.get<MeetingUser>('/app_tx_meeting/meeting-user/personal')
    },
    /**
     * @desc 修改会议预定的用户角色 (管理员权限)
     * @method POST
     * @url /app_tx_meeting/meeting-user/{meeting_user_id}/role
     * @param {string} meetingUserId - 会议用户ID
     * @param {UserRole} role - 用户角色
     */
    setUserRole: (meetingUserId: string, role: UserRole) => {
      return axiosService.post<{
        role: UserRole
      }>(`/app_tx_meeting/meeting-user/${meetingUserId}/role`, null, { params: { role } })
    },
    /**
     * @desc 修改会议预定的用户状态 (管理员权限)
     * @method POST
     * @url /app_tx_meeting/meeting-user/{meeting_user_id}/status
     * @param {string} meetingUserId - 会议用户ID
     * @param {UserStatus} status - 用户状态
     */
    setUserStatus: (meetingUserId: string, status: UserStatus) => {
      return axiosService.post<{
        status: UserStatus
      }>(`/app_tx_meeting/meeting-user/${meetingUserId}/status`, null, { params: { status } })
    },
    /**
     * @desc 会议预定的用户信息修改 (用户个人或管理员权限)
     * @method PUT
     * @url /app_tx_meeting/meeting-user/{meeting_user_id}
     * @param {string} meetingUserId - 会议用户ID
     * @param {MeetingUserUpdate} data - 用户信息数据
     */
    updateMeetingUser: (meetingUserId: string, data: MeetingUserUpdate) => {
      return axiosService.put<MeetingUser>(`/app_tx_meeting/meeting-user/${meetingUserId}`, data)
    },
    /**
     * @desc 列举会议预定
     * @method GET
     * @url /app_tx_meeting/meeting/reservation
     * @param {ListReservationsParams} params - 查询参数
     */
    listReservations: (params?: ListReservationsParams) => {
      return axiosService.get<ListReservationsResponse>('/app_tx_meeting/meeting/reservation', { params })
    },
    /**
     * @desc 创建会议预定
     * @method POST
     * @url /app_tx_meeting/meeting/reservation
     * @param {CreateReservationPayload} data - 会议预定数据
     */
    createReservation: (data: CreateReservationPayload) => {
      // 响应体是单个 MeetingReservation
      return axiosService.post<MeetingReservation>('/app_tx_meeting/meeting/reservation', data)
    },
    /**
     * @desc 查询会议预定详情
     * @method GET
     * @url /app_tx_meeting/meeting/reservation/{id}
     * @param {string} id - 会议预定ID
     * @param {boolean} asAdmin - 是否以管理员身份请求
     */
    getReservationDetail: (id: string, asAdmin?: boolean) => {
      // 响应体是单个 MeetingReservation，可能包含 email_notice
      const params = asAdmin ? { 'as-admin': true } : undefined
      return axiosService.get<MeetingReservation>(`/app_tx_meeting/meeting/reservation/${id}`, { params })
    },
    /**
     * @desc 取消会议预定
     * @method POST
     * @url /app_tx_meeting/meeting/reservation/{id}/cancel
     * @param {string} id - 会议预定ID
     */
    cancelReservation: (id: string) => {
      // 响应体为空
      return axiosService.post<void>(`/app_tx_meeting/meeting/reservation/${id}/cancel`)
    },
    /**
     * @desc 会议预定会前通知时间设置
     * @method POST
     * @url /app_tx_meeting/meeting/reservation/{id}/notice-time
     * @param {string} id - 会议预定ID
     * @param {number} advanceMinutes - 提前分钟数
     */
    setReservationNoticeTime: (id: string, advanceMinutes: number) => {
      // 响应体为空
      return axiosService.post<void>(`/app_tx_meeting/meeting/reservation/${id}/notice-time`, null, { params: { advance_minutes: advanceMinutes } })
    },
    /**
     * @desc 查询会议预定须知
     * @method GET
     * @url /app_tx_meeting/reserve/terms-conditions
     */
    getTermsConditions: () => {
      return axiosService.get<TermsConditionsPayload>('/app_tx_meeting/reserve/terms-conditions')
    },
    /**
     * @desc 更新会议预定须知 (管理员权限)
     * @method POST
     * @url /app_tx_meeting/reserve/terms-conditions/update
     * @param {TermsConditionsPayload} data - 须知内容
     */
    updateTermsConditions: (data: TermsConditionsPayload) => {
      return axiosService.post<TermsConditionsPayload>('/app_tx_meeting/reserve/terms-conditions/update', data)
    },
    /**
     * @desc 列举腾讯企业会议
     * @method GET
     * @url /app_tx_meeting/v1/corp/meetings
     * @param {GetTencentMeetingInfoParams} params - 查询参数
     */
    getTencentMeetingInfo: (params: GetTencentMeetingInfoParams) => {
      return axiosService.get<GetTencentMeetingInfoResponse>('/app_tx_meeting/v1/corp/meetings', { params })
    },
    /**
     * @desc 通过会议号查询腾讯企业会议信息
     * @method GET
     * @url /app_tx_meeting/v1/corp/meetings/code
     * @param {string} meetingCode - 会议号
     */
    getMeetingByCode: (meetingCode: string) => {
      return axiosService.get<GetMeetingByCodeResponse>('/app_tx_meeting/v1/corp/meetings/code', { params: { meeting_code: meetingCode } })
    }
  }
}
